package xyz.yeidi.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import xyz.yeidi.pojo.Deliveryaddress;
import xyz.yeidi.service.api.DeliveryAddressService;
import xyz.yeidi.utils.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author JIP
 * @Date 2021-08-06 12:17
 * @Version 1.0
 * @Describe :
 */
@WebServlet("/getAllAddress")
public class GetAllAddressServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DeliveryAddressService deliveryAddressService = ServiceFactory.getDeliveryAddressService();
        String userId = request.getParameter("userId");
        List<Deliveryaddress> addresses = deliveryAddressService.getAllAddressByUserId(Integer.valueOf(userId));
        ObjectMapper om = new ObjectMapper();
        PrintWriter out = response.getWriter();
        if(addresses == null){
            out.print("no_address");
        }else{
            out.print(om.writeValueAsString(addresses));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
